package com.dbgs.insurance.insuredperson.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.hss.base.BaseActivty;


/**
 * Created by Administrator on 2017/3/8.
 */

public class SplashActivity extends BaseActivty {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initJpush();
        goNextActivity();
    }

    @Override
    protected void onResume() {
        super.onResume();
       // checkVersion();
    }

    /*void checkVersion(){
            addSubscription(ApiManager.getInstance().getmApi().versionCheck(
                    ReqCreateParams.requestBaseParams(GlobalCache.getInstance().getReqBaseParams())),
                    new ApiCallback<ResVerionCheck>() {
                @Override
                public void onSuccess(ResVerionCheck result) {
                    processVersion(result);

                }

                @Override
                public void onFailure(String msg) {
                    processVersion(null);

                }

                @Override
                public void onFinish() {

                }

                @Override
                public void onDataErr(String msg) {
                    processVersion(null);
                }

                        @Override
                        public void onExitLogin() {

                        }

                    });
    }

    void processVersion(ResVerionCheck result){
        if(result !=null){
            int curVersionCode = CommonUtil.getAppVersionCode(getApplicationContext());
            int serVerCode = result.verCode;
            if (curVersionCode <serVerCode){
                showVersionUpdate(result);
                return;
            }
        }
        goNextActivity();
    }

    MaterialDialog versionUpdateDialog;
    void showVersionUpdate(final ResVerionCheck version) {
        if (isFinishing()) {
            return;
        }

        StringBuilder sb = new StringBuilder();
        sb.append("最新版本：");
        sb.append(version.ver);
        sb.append("\n\n");
        sb.append(version.updateNote);

        versionUpdateDialog = MaterialDialogsHelper.showDialog(SplashActivity.this, "版本更新", sb.toString(), "立即升级", (version.updateForce != null && version.updateForce == 1) ? "退出" : "暂不升级", new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                dialog.dismiss();
                if (DialogAction.POSITIVE == which) {
                    Intent intent1 = new Intent();
                    intent1.setAction("android.intent.action.VIEW");
                    Uri content_url = Uri.parse(version.link);
                    intent1.setData(content_url);
                    startActivity(intent1);
                    exitFunction();
                } else {
                    if (version.updateForce != null && version.updateForce == 1) {
                        exitFunction();
                    } else {
                        goNextActivity();
                    }
                }
            }
        });
        versionUpdateDialog.setCancelable(false);
    }*/

    private void goNextActivity() {
       // String userId = GlobalCache.getInstance().getUserId();
        /*if (TextUtils.isEmpty(userId)){
            startActy(LoginActivity.class);
        }else {
            startActy(MainActivity.class);
        }*/
        startActy(MainActivity.class);
        finishActivity();
    }

    void initJpush(){

       /* Set<String> tags = new HashSet<>();
        String tag = BuildConfig.DEBUG ? "t_" : "p_";
        tags.add(tag+"android");

        JPushInterface.setTags(getApplicationContext(), tags, new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {
                LogUtils.e( "JsetTags = " + (set !=null ? set.size() : 0));

            }
        });
        boolean isJpushEnable = XmlPerference.getInstance(getApplicationContext()).getKeyBooleanValue(ConstStr.KEY_PUSH_ENABLE, true);

        if (isJpushEnable){
            JPushInterface.resumePush(getApplicationContext());
        }else {
            JPushInterface.stopPush(getApplicationContext());
        }*/
    }

}
